package com.cxj.musicplayer.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cxj.musicplayer.R;
import com.cxj.musicplayer.adapter.LocalSongListAdapter;
import com.cxj.musicplayer.bean.Song;
import com.cxj.musicplayer.dao.SongDBUtil;
import com.cxj.musicplayer.data.ActivityManager;
import com.cxj.musicplayer.data.DataManager;

import java.util.List;

public class LocalListActivity extends TitleActivity {
    private ListView listView;
    private List<Song> songs;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_list);

        listView = findViewById(R.id.local_songs_list);
        SongDBUtil songDBUtil = new SongDBUtil(getBaseContext(), "music_db", null, 1);
        songs = songDBUtil.queryAllSongs();
        LocalSongListAdapter adapter = new LocalSongListAdapter(getBaseContext(), songs);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new ListView.OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final String song_id = songs.get(i).getSong_id();
                Log.i(getLocalClassName() + ":song_id", song_id);
                Intent intent = new Intent(LocalListActivity.this, OnPlaying.class);
                intent.putExtra("song_id", song_id);
                intent.putExtra("song_i", i);
                intent.putExtra("song_source", "l_");
                ActivityManager.pre = SongsListActivity.class;
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SongDBUtil songDBUtil = new SongDBUtil(getBaseContext(), "music_db", null, 1);
        songs = songDBUtil.queryAllSongs();
        LocalSongListAdapter adapter = new LocalSongListAdapter(getBaseContext(), songs);
        listView.setAdapter(adapter);
    }
}
